<?php
/*
 * Custom AppModel for rdBloggery
 *
 * @author      gwoo <gwoo@rd11.com>
 * @version     0.10.8.2176
 * @license		OPPL
 *
 */
class AppModel extends Model {

	var $useDbConfig = 'default';
    var $encrypt_col = array();
	/**
	 * move method will adjust a given field based on the value passed
	 *
	 * @id is the primaryKey of the row to be moved
	 * @name of the field that should be modified
	 * @value which way and how far, for example: -1 or +1
	 * @validate it?
	 * @return void
	 * @author gwoo
	 **/
	function deleteMulti($condition)
	{
		$this->query("delete from `{$this->table}` WHERE $condition");		
	}
	
	/**
	 * @auth yangjie
	 * @desc 在保存数据前做排他处理，同时做加密处理
	 */
    function beforeSave(){
    	$modified = 'modified';
    	
		if(!empty($this->data[$this->name]['id']) && isset($this->data[$this->name][$modified])){
			$find_data = $this->findById($this->data[$this->name]['id']);				
			if(!empty($find_data)){
				if($this->data[$this->name][$modified] != $find_data[$this->name][$modified]){
					return false;
				}
				else{					
					unset($this->data[$this->name][$modified]);															
				}
			}
		}
		else{
			$this->data[$this->name][$modified]=date('Y-m-d H:i:s');			
		}
		
		$this->encode($this->data); //数据加密处理
		return true;
    }
    
    /**
     * @auth yangjie
     * @desc 得到查询数据后做解密处理
     */
    function afterFind($results){
    	if(!empty($results)){
    		$this->decode($results);
    	}
    	return $results;
    }
    
    /**
     * @auth yangjie
     * @desc 数据加密方法
     */
    function encode(&$data){
    	if(!empty($this->encrypt_col)){
	    	foreach($this->encrypt_col as $key=>$val){
	    		if(isset($data[$this->name][$val])){
		    		$encrypt = new EncryptComponent();
					$encrypt->encode($data[$this->name][$val]);
	    		}    		
	    	} 
    	}
    }
    
    /**
     * @auth yangjie
     * @desc 数据解密方法
     */
    function decode(&$data){
    	if(isset($this->encrypt_col) && !empty($this->encrypt_col)){
    		foreach ($data as $key => $val){
    			if(!empty($key) && $this->name == $key){//解密一条记录 
					$this->decode_array($data);  
					break;  				
    			}		
				foreach($val as $k => $v){//解密多条记录
					if($this->name==$k){
						$this->decode_array($data[$key]);    				
	    			}
				}
    		}
    	}
    }
    
    /**
     * @auth yangjie
     * @desc 数组解密方法
     */
    function decode_array(&$data){
    	if(!empty($this->encrypt_col)){
	    	foreach($this->encrypt_col as $key=>$val){
	    		$encrypt = new EncryptComponent();
				$encrypt->decode($data[$this->name][$val]);    		
	    	} 
    	}
    }
	
    /**
     * 输出变量的内容，用于调试
     *
     * @author wang.wei
     *
     * @param mixed $vars 要输出的变量
     * @param string $label
     * @param boolean $return
     */
    function formatChars($vars, $label = '', $return = false)
    {
    	if (ini_get('html_errors')) {
    		$content = "<pre>\n";
    		if ($label != '') {
    			$content .= "<strong>{$label} :</strong>\n";
    		}
    		$content .= htmlspecialchars(print_r($vars, true));
    		$content .= "\n</pre>\n";
    	} else {
    		$content = $label . " :\n" . print_r($vars, true);
    	}
    	if ($return) {
    		return $content;
    	}
    	echo $content;
    	return null;
    }
	
    /**
     * 输出变量的内容(版本2)，用于调试
     *
     * @author tsubasa
     *
     * @param $data 要输出的变量
     */
    function print_fmt($data = array(), $tab_count = 0) {
    	if(is_array($data)) {
    		echo "(<br/>";
    		$tab_count += 1;
    		foreach($data as $key => $val) {
    			for($i = 0; $i < $tab_count; $i++) {
    				echo '&nbsp;&nbsp;&nbsp;&nbsp;';
    			}
    			echo '['.$key.']';
    			echo '&nbsp;=&gt;&nbsp;';
    			$this->print_fmt($val, $tab_count);
    			echo "<br/>";
    		}
    		for($i = 0; $i < $tab_count - 1; $i++) {
    			echo '&nbsp;&nbsp;&nbsp;&nbsp;';
    		}
    		echo ")";
    	} else {
    		echo '"'.str_ireplace("<br>", "", str_ireplace("<br/>", "", $data)).'"';
    	}
    }
    
}

?>